﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Csla;

namespace Model
{
    [Serializable()]
    public class DPKVBOCollection : Csla.BusinessListBase<DPKVBOCollection, DPKVBO>
    {
        public GiaDatChuanBOList giaDatChuanBOList = null;

        #region BindingList Overrides
        protected override object AddNewCore()
        {
            DPKVBO _DPKVBO = DPKVBO.NewChildDPKVBO();
            OnAddingNew(new System.ComponentModel.AddingNewEventArgs(_DPKVBO));
            Add(_DPKVBO);
            return _DPKVBO;
        }
        #endregion BindingList Overrides
        //==========================================================================

        #region Factory Methods
        private DPKVBOCollection()
        {
        }

        public static DPKVBOCollection NewDPKVBOCollection()
        {
            return new DPKVBOCollection();
        }

        public static DPKVBO NewDPKVBO()
        {
            return DPKVBO.NewChildDPKVBO();
        }

        public static DPKVBOCollection GetDPKVBOCollection()
        {
            SPParam spParam = new SPParam("DPID", "ASC", "WHERE DELE=0");
            return DataPortal.Fetch<DPKVBOCollection>(spParam);
        }

        public static DPKVBOCollection GetDPKVBOCollection(bool getDeleted)
        {
            SPParam spParam = new SPParam("DPID", "ASC", "");
            return DataPortal.Fetch<DPKVBOCollection>(spParam);
        }

        public static DPKVBOCollection GetDPKVBOCollection(string where)
        {
            SPParam spParam = new SPParam("DPID", "ASC", "WHERE DELE=0" + (where != "" ? " AND " : "") + where);
            return DataPortal.Fetch<DPKVBOCollection>(spParam);
        }

        public static DPKVBOCollection GetDPKVBOCollection(string where, bool getDeleted)
        {
            SPParam spParam = new SPParam("DPID", "ASC", (where != "" ? "WHERE " : "") + where);
            return DataPortal.Fetch<DPKVBOCollection>(spParam);
        }

        public static DPKVBOCollection GetDPKVBOCollection(string where, string orderByColumn)
        {
            SPParam spParam = new SPParam((orderByColumn == "" ? "DPID" : orderByColumn), "ASC", "WHERE DELE=0" + (where != "" ? " AND " : "") + where);
            return DataPortal.Fetch<DPKVBOCollection>(spParam);
        }

        public static DPKVBOCollection GetDPKVBOCollection(string where, string orderByColumn, bool getDeleted)
        {
            SPParam spParam = new SPParam((orderByColumn == "" ? "DPID" : orderByColumn), "ASC", (where != "" ? "WHERE " : "") + where);
            return DataPortal.Fetch<DPKVBOCollection>(spParam);
        }

        public static DPKVBOCollection GetDPKVBOCollection(string where, string orderByColumn, string orderByExpression)
        {
            SPParam spParam = new SPParam((orderByColumn == "" ? "DPID" : orderByColumn), (orderByExpression == "" ? "ASC" : orderByExpression), "WHERE DELE=0" + (where != "" ? " AND " : "") + where);
            return DataPortal.Fetch<DPKVBOCollection>(spParam);
        }

        public static DPKVBOCollection GetDPKVBOCollection(string where, string orderByColumn, string orderByExpression, bool getDeleted)
        {
            SPParam spParam = new SPParam((orderByColumn == "" ? "DPID" : orderByColumn), (orderByExpression == "" ? "ASC" : orderByExpression), (where != "" ? "WHERE " : "") + where);
            return DataPortal.Fetch<DPKVBOCollection>(spParam);
        }
        #endregion Factory Methods
        //==========================================================================

        #region Criteria
        [Serializable()]
        private class Criteria
        {
            public Criteria()
            {
            }
        }

        [Serializable()]
        public class FilterCriteria
        {
            public FilterCriteria()
            {
            }
        }
        #endregion Criteria
        //==========================================================================

        #region Fetch
        [Transactional(TransactionalTypes.TransactionScope)]
        private void DataPortal_Fetch(SPParam spParam)
        {
            RaiseListChangedEvents = false;
            this.giaDatChuanBOList = GiaDatChuanBOList.GetGiaDatChuanBOList("VTRI=1 AND GIAC IS NOT NULL");

            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("DPKVBO_SelectBOCollection");
            db.AddInParameter(cmd, "@OrderColumn", SqlDbType.VarChar, (object)spParam.orderColumn);
            db.AddInParameter(cmd, "@OrderExpression", SqlDbType.VarChar, (object)spParam.orderExpression);
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, (object)spParam.whereExpression);
            using (SqlDataReader dr = (SqlDataReader)db.ExecuteReader(cmd))
            {
                int stt = 0;

                while (dr.Read())
                {
                    DPKVBO objDPKVBO = DPKVBO.GetChildDPKVBO(dr);
                    objDPKVBO.STT0 = ++stt;
                    if (objDPKVBO.NHGD != null && objDPKVBO.HESO != null)
                        if (this.giaDatChuanBOList.ContainsKey(objDPKVBO.NHGD.Value))
                        {
                            objDPKVBO.GIACHUAN = this.giaDatChuanBOList.Value(objDPKVBO.NHGD.Value);
                            objDPKVBO.GIA1M2 = objDPKVBO.GIACHUAN.Value * objDPKVBO.HESO.Value;
                        }

                    this.Add(objDPKVBO);
                }
            }
            RaiseListChangedEvents = true;
        }
        #endregion Fetch
        //==========================================================================

        #region Update
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            Update();
        }

        internal void Update()
        {
            RaiseListChangedEvents = false;
            foreach (DPKVBO child in this)
            {
                if (child.IsNew)
                    child.Insert();
                else
                    child.Update();
            }
            RaiseListChangedEvents = true;
        }
        #endregion Update
        //==========================================================================

        #region Business Method
        public DPKVBO GetDPKVBO(int _DPID)
        {
            DPKVBO returnObject = null;
            foreach (DPKVBO _DPKVBO in this)
            {
                if (_DPKVBO.DPID == _DPID)
                {
                    returnObject = _DPKVBO;
                    break;
                }
            }
            return returnObject;
        }

        public void Remove(int DPID)
        {
            foreach (DPKVBO _DPKVBO in this)
            {
                if (_DPKVBO.DPID == DPID)
                {
                    try
                    {
                        this.Remove(_DPKVBO);
                    }
                    catch { }
                    break;
                }
            }
        }

        public DPKVBOCollection GetDPKVBOCollectionByTDID(int TDID)
        {
            DPKVBOCollection dpkvBOCollection = DPKVBOCollection.NewDPKVBOCollection();

            if (TDID != -1)
            {
                for (int i = 0; i < this.Count; i++)
                {
                    if (this[i].TDID == TDID)
                        dpkvBOCollection.Add(this[i]);
                }
            }

            return dpkvBOCollection;
        }
        #endregion Business Method
        //==========================================================================
    }
}
